from pydantic import BaseModel, Field
from typing import Optional, Generic, TypeVar

T = TypeVar('T')

class Token(BaseModel):
    access_token: str
    token_type: str
    refresh_token: Optional[str] = None
    expires_in: Optional[int] = None


class TokenData(BaseModel):
    login_name: Optional[str] = None  # 登录名
    full_name: Optional[str] = None  # 完整姓名
    tenant_id: Optional[str] = None  # 租户ID
    roles: Optional[list[str]] = None  # 角色列表


class LoginRequest(BaseModel):
    login_name: str = Field(..., description="登录名")
    password: str = Field(..., description="密码")
    tenant_id: str = Field(..., description="租户ID")
    
    # 兼容旧版API
    @property
    def username(self) -> str:
        return self.login_name


class APIResponse(BaseModel, Generic[T]):
    code: int
    msg: str
    data: Optional[T] = None
    
    @classmethod
    def success(cls, data: T = None, msg: str = "操作成功") -> 'APIResponse[T]':
        """创建成功响应"""
        return cls(code=200, msg=msg, data=data)
    
    @classmethod
    def error(cls, code: int = 400, msg: str = "操作失败", data: T = None) -> 'APIResponse[T]':
        """创建错误响应"""
        return cls(code=code, msg=msg, data=data)
    
    @classmethod
    def not_found(cls, msg: str = "资源不存在", data: T = None) -> 'APIResponse[T]':
        """创建404响应"""
        return cls(code=404, msg=msg, data=data)
    
    @classmethod
    def bad_request(cls, msg: str = "请求错误", data: T = None) -> 'APIResponse[T]':
        """创建400响应"""
        return cls(code=400, msg=msg, data=data)